package com.lineage.server.command.executor;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Chat implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1Chat.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1Chat();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      StringTokenizer st = new StringTokenizer(arg);
      if (st.hasMoreTokens()) {
        String msg, flag = st.nextToken();
        if (flag.compareToIgnoreCase("on") == 0) {
          World.get().set_worldChatElabled(true);
          msg = "取消广播限制。";
        } else {
          if (flag.compareToIgnoreCase("off") != 0)
            throw new Exception(); 
          World.get().set_worldChatElabled(false);
          msg = "设置广播限制。";
        } 
        if (pc == null) {
          _log.warn("系统命令执行: " + cmdName + " " + arg + " " + msg);
        } else {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(msg));
        } 
      } else {
        String msg2;
        if (World.get().isWorldChatElabled()) {
          msg2 = "目前未暂停广播使用。.chat off 可以设置广播限制。";
        } else {
          msg2 = "目前暂停广播使用。.chat on 可以取消广播限制。";
        } 
        if (pc == null) {
          _log.warn("指令异常: " + msg2);
        } else {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(msg2));
        } 
      } 
    } catch (Exception e) {
      if (pc == null) {
        _log.error("错误的命令格式: " + getClass().getSimpleName());
      } else {
        _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
      } 
    } 
  }
}
